/**
* 图片处理工具
*/

(function(win, $){
    var module = {};

    /**
    * 初始化图片选择对象
    * 适用于h5
    */
    module.inputInit = function(parentDom, changeCallBack){
        parentDom && parentDom.append("<input class = 'file-input' type = 'file' accept='image/*'/>");
    }

    /**
    * 图片选择
    * 适用mui h5+
    */
    module.captureImage = function(captureCallBack){
        if(!plus){return false;}
        plus.gallery.pick(function(e) {
            var fileSrc = "";
            for(var i in e.files) {
                fileSrc = e.files[i]
            }
            var img = new Image();
            img.src = fileSrc;
            img.onload = function() {
                var picdata = module.getBase64Image(img, null, null, 400);
                captureCallBack(picdata)
            }
        }, function(e) {
            console.log("取消选择图片");
        }, {
            filter: "image",
            multiple: true,
            maximum: 1,
            system: false,
            onmaxed: function() {
                plus.nativeUI.alert('最多只能选择1张图片');
            }
        });
    }

    /**
    * 照相选择图片
    * 适用mui h5+
    */
    module.captureCameraImage = function(captureCallBack){
        var cmr = plus.camera.getCamera();
        var res = cmr.supportedImageResolutions[0];
        var fmt = cmr.supportedImageFormats[0];
        cmr.captureImage(function(path) {
            plus.io.resolveLocalFileSystemURL(path, function(entry) {
                var img = new Image();
                img.src = entry.toLocalURL();
                img.onload = function() {
                    var picdata = module.getBase64Image(img, null, null, 400);
                    captureCallBack(picdata)
                 }
            }, function() {
                alert("读取拍照文件错误！")
            })
            },function(error) {
                alert("Capture image failed: " + error.message);
            },{
                resolution: res,
                format: fmt
            }
        );
    }


    /**
    * 通过图片路径转为base64
    * @params
    * imgPath 图片路径
    * callback 图片处理完成回调  callback(imgData)
    * width 图片宽度
    * height 图片高度
    * scallWidth 图片等比列缩放宽度，非正方形:以宽>高 = 宽  高>宽 = 高
    * quality 图片质量压缩
    * x 图片渲染偏移x
    * y 图片渲染偏移y
    */ 
    module.getImageBase64ByPath = function(imgPath, callback, width, height, scallWidth, quality, x, y){
        var image = new Image();
        image.src = imgPath || "";
        image.style.width = width || "100%";
        image.style.height = height || "100%";

        image.onload = function(){
            var imgData = module.getBase64Image(image, width, height, scallWidth, quality, x, y);
            callback(imgData)
        }
    }

    /**
    * 图片压缩转为base64
    * @params
    * image 图片对象
    * minWidth 图片宽度设置
    * minHeight 图片高度设置
    * scallWidth 图片等比列缩放宽度，非正方形:以宽>高 = 宽  高>宽 = 高
    * quality 图片质量压缩
    * x 图片渲染偏移x
    * y 图片渲染偏移y
    */
    module.getBase64Image = function(image, minWidth, minHeight, scallWidth, quality, x, y){
        var canvas = document.createElement("canvas");
        var width = minWidth || image.width;
        var height = minHeight || image.height;
        // calculate the width and height, constraining the proportions 
        if(scallWidth){
            if(width > height) {
                if(width > scallWidth) {
                    height = Math.round(height *= scallWidth / width);
                    width = scallWidth;
                }
            } else {
                if(height > scallWidth) {
                    width = Math.round(width *= scallWidth / height);
                    height = scallWidth;
                }
            }
        }
        
        canvas.width = width; /*设置新的图片的宽度*/
        canvas.height = height; /*设置新的图片的长度*/
        var ctx = canvas.getContext("2d");
        ctx.drawImage(image, x||0, y||0, width, height); /*绘图*/
        var dataURL = canvas.toDataURL("image/png", quality || 1);
        return dataURL.replace("data:image/png;base64,", "");
    }


    win.imageUnitl = module;
})(window, jQuery)